package cn.edu.scau.cmi.ema.dao;

import cn.edu.scau.cmi.ema.base.CmiPagination;
import cn.edu.scau.cmi.ema.domain.Hazard;
import cn.edu.scau.cmi.ema.util.CmiSqlUtil;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

@Repository("HazardDAO")
@Transactional
public class HazardDAOImpl extends AbstractJpaDao<Hazard> implements HazardDAO {

	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(
			Arrays.asList(new Class<?>[] { Hazard.class }));

	@PersistenceContext()
	private EntityManager entityManager;

	public HazardDAOImpl() {
		super();
	}

	public EntityManager getEntityManager() {
		return entityManager;
	}

	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	@Transactional
	public Hazard findHazardById(Integer id) throws DataAccessException {

		return findHazardById(id, -1, -1);
	}

	@Transactional
	public Hazard findHazardById(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findHazardById", startResult, maxRows, id);
			return (Hazard) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Hazard> findHazardByName(String name) throws DataAccessException {

		return findHazardByName(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Hazard> findHazardByName(String name, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findHazardByName", startResult, maxRows, name);
		return new LinkedHashSet<Hazard>(query.getResultList());
	}

	@Transactional
	public Set<Hazard> findHazardByNameContaining(String name) throws DataAccessException {

		return findHazardByNameContaining(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Hazard> findHazardByNameContaining(String name, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findHazardByNameContaining", startResult, maxRows, name);
		return new LinkedHashSet<Hazard>(query.getResultList());
	}

	@Transactional
	public Set<Hazard> findHazardByDescription(String description) throws DataAccessException {

		return findHazardByDescription(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Hazard> findHazardByDescription(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findHazardByDescription", startResult, maxRows, description);
		return new LinkedHashSet<Hazard>(query.getResultList());
	}

	@Transactional
	public Set<Hazard> findHazardByDescriptionContaining(String description) throws DataAccessException {

		return findHazardByDescriptionContaining(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Hazard> findHazardByDescriptionContaining(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findHazardByDescriptionContaining", startResult, maxRows, description);
		return new LinkedHashSet<Hazard>(query.getResultList());
	}

	@Transactional
	public Hazard findHazardByPrimaryKey(Integer id) throws DataAccessException {
		return findHazardByPrimaryKey(id, -1, -1);
	}

	@Transactional
	public Hazard findHazardByPrimaryKey(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findHazardByPrimaryKey", startResult, maxRows, id);
			return (Hazard) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Hazard> findHazardsByIds(Set<Integer> ids) throws DataAccessException {
		try {
			Query query = createNamedQuery("findHazardsByIds", -1, -1, ids);
			return new HashSet(query.getResultList());
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Hazard> findAllHazards() throws DataAccessException {

		return findAllHazards(-1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Hazard> findAllHazards(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAllHazards", startResult, maxRows);
		return new LinkedHashSet<Hazard>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Hazard> findHazardByCompositeProperities(String sql, int startResult, int maxRows) {
		return new LinkedHashSet<Hazard>(createQuery(sql, startResult, maxRows).getResultList());
	}

	@Transactional
	public Set<Hazard> findHazardsByDetachedCriteria(CmiPagination page) throws DataAccessException {
		DetachedCriteria detachedCriteria = CmiSqlUtil.createDetachedCriteria(page, Hazard.class);
		Session session = (Session) entityManager.getDelegate();
		Set<Hazard> hazards = new HashSet(detachedCriteria.getExecutableCriteria(session)
				.setFirstResult(page.getSearchBeginCursor()).setMaxResults(page.getPaginationSize()).list());

		return hazards;
	}

	@Transactional
	public Hazard findHazardByPhysicalPrimaryKey(String name) throws DataAccessException {
		return findHazardByPhysicalPrimaryKey(name, -1, -1);
	}

	@Transactional
	public Hazard findHazardByPhysicalPrimaryKey(String name, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findHazardByPhyscialPrimaryKey", startResult, maxRows, name);
			return (Hazard) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	public boolean canBeMerged(Hazard entity) {
		return true;
	}
}
